package com.pingtai.ajax.action;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpUtils;

import net.sf.json.JSONObject;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.TextExtractingVisitor;
import org.springframework.http.HttpMethod;

import com.mysql.jdbc.Util;
import com.pingtai.ajax.entity.IndentTarget_163;
import com.pingtai.util.ChangeCharset;
import com.pingtai.util.DummyRedirectHandler;
import com.pingtai.util.HttpClientUtils;
import com.pingtai.util.Utils;
import com.pingtai.xinlang.login.login.WeiboLoginService;
import com.sun.net.httpserver.Filter;

public class test {

	
	public static List<Long> getDigit(String text) {
	    List<Long> digitList = new ArrayList<Long>();
	    String[] arr = text.split("[^\\d]+");
	    for (String str : arr) {
	        if (str.length() > 0)
	            digitList.add(Long.valueOf(str));
	    }
	    return digitList;
	}
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		WeiboLoginService loginService = new WeiboLoginService();
		loginService.getLoginStatus("smxread2@163.com","FuDan2003");
//		
//		HttpClient client = HttpClientUtils.getHttpClient();
//		HttpGet getMethod = new HttpGet("http://weibo.com/sojava");  
//        
//        //下面一行需要注释掉，不然返回数据是gzip压缩后的乱码  
//        //getMethod.setRequestHeader("Accept-Encoding","gzip, deflate");  
//        getMethod.setHeader("Accept-Language","zh-cn");  
//        getMethod.setHeader("Connection","Keep-Alive");  
//        getMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 QIHU 360SE");
//        getMethod.setHeader("Host","weibo.com");  
//        getMethod.setHeader("Cookie","SUE=es%3D7aed3acb64e3b48eb9ca570f19086f4d%26ev%3Dv1%26es2%3Da48fd9f042c3ae035437ddd1bcd9e5cb%26rs0%3DwFKg4%252Bo0ruCy2sgDsNpLT2%252BADI7grbiTBBBXWLsbPqProWlnjH%252FcgPMc5wXPdFW%252FGi9fHOfMrtRw%252B1dYGnTDNLL8l%252BgWw%252FORmrOhxw8HEPCefUj5TzL5C1CFCRcFBasISAJSJY98CR4BxKGLYQN43pzwq1rcAnk56mcT1vB9Prw%253D%26rv%3D0;SUP=cv%3D1%26bt%3D1388655492%26et%3D1388741892%26d%3D40c3%26i%3D2bf4%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D0%26st%3D0%26lt%3D1%26uid%3D3945635581%26user%3Dsmxread2%2540163.com%26ag%3D4%26name%3Dsmxread2%2540163.com%26nick%3D%25E5%25B0%258F8%25E9%25A9%25AC%25E5%2593%25A5%26sex%3D1%26ps%3D0%26email%3D%26dob%3D%26ln%3Dsmxread2%2540163.com%26os%3D%26fmp%3D%26lcp%3D;"); 
//        HttpResponse response = client.execute(getMethod);
//        InputStream in = response.getEntity().getContent();
//        BufferedInputStream bin = new BufferedInputStream(in);
//        String msg = Utils.InputStreamTOString(in, "utf-8");
//        System.out.println(msg);
//        List<Cookie> cookies = ((AbstractHttpClient) client).getCookieStore()
//				.getCookies();
//
//		String s = "";
//		boolean sue = false;
//		boolean sup = false;
//		System.out.println(cookies.size());
//		for (int i = 0; i < cookies.size(); i++) {
//			Cookie cookie = cookies.get(i);
//			String name = cookie.getName();
////			if(name.equalsIgnoreCase("sue")&&!sue){
////				s += name + "="+cookie.getValue()+";";
////				sue = true;
////			}else if(name.equalsIgnoreCase("sup")&&!sup){
//				s += name + "="+cookie.getValue()+";";
////				sup = true;
////			}
//		}
//		System.out.println(s);
//		Parser parser = Parser.createParser(msg, "utf-8");
//		NodeFilter filter = new HasAttributeFilter("name", "uid");
//		NodeList list = parser.extractAllNodesThatMatch(filter);
//		
//
//		if (list != null) {
//			for(int i=0;i<list.size();i++){
//				Node textnode = (Node) list.elementAt(i);
//				String a = textnode.toHtml();
//				a=a.substring(a.indexOf("value=")+7,a.lastIndexOf("\""));
//				System.out.println(a);
//			}
//		}
        //			String msg = weiboLoginService.getConent(url, client);
//		//解析
//		msg = Utils.unicodeToString(msg);	
////		getIndentTargetInfo_qq("http://t.qq.com/p/t/347813013853077");


	    }
	 private static void message( String szMsg ) {
//	        try{System.out.println(new String(szMsg.getBytes("utf-8"), System.getProperty("file.encoding"))); } catch(Exception e ){}
	    }
	 public static IndentTarget_163 getIndentTargetInfo_qq(String targetUrl)
				throws IOException, ParserException {
		 URL url;

			url = new URL(targetUrl);
			URLConnection urlConnection = url.openConnection();

			InputStream inputStream = urlConnection.getInputStream();
			String s = ChangeCharset.inputStreamToString(inputStream, "utf-8");

			Parser parser = Parser.createParser(s, "utf-8");
			NodeFilter msgFilter = new HasAttributeFilter("id","qzShareCont");
			String msg = "";
			NodeList nodes = parser.extractAllNodesThatMatch(msgFilter);
			if (nodes != null) {
				for (int i = 0; i < nodes.size(); i++) {
					 Node textnode = (Node) nodes.elementAt(i);
		              NodeList l = textnode.getChildren();
		              Node n = textnode.getFirstChild();
		              msg=n.toHtml();
		              msg = Utils.NoString(msg);
				}
			}
			
			String aid = targetUrl.replace("http://t.qq.com/p/t/", "");
			String zbUrlStr = "http://api.t.qq.com/old/message.php?id="+aid+"&format=1&filter=5";
			
			URL zbUrl = new URL(zbUrlStr);
			URLConnection zbUrlConnection = zbUrl.openConnection();
			InputStream zbInputStream = zbUrlConnection.getInputStream();
			String zbS = ChangeCharset.inputStreamToString(zbInputStream, "utf-8");
			zbS = Utils.unicodeToString(zbS);
			JSONObject jsonObject = JSONObject.fromObject(zbS);
			zbS = (String) jsonObject.get("info");
			
			Parser zbParser = Parser.createParser(zbS, "utf-8");
			NodeFilter zbFilter = new HasAttributeFilter("class","select");
			String zbNumber="";
			NodeList zbNodes = zbParser.extractAllNodesThatMatch(zbFilter);
			if(zbNodes!=null) {
	          for (int i = 0; i < zbNodes.size(); i++) {
	              Node textnode = (Node) zbNodes.elementAt(i);
	              NodeList l = textnode.getChildren();
	              Node n = textnode.getFirstChild();
	              zbNumber=n.toHtml();
	              zbNumber = Utils.NoString(zbNumber);
	              zbNumber = zbNumber.replace("转播", "");
	              zbNumber = zbNumber.replace("(", "");
	              zbNumber = zbNumber.replace(")", "");
	          }
	      } 
			
			String plUrlStr = "http://api.t.qq.com/old/message.php?id="+aid+"&format=1&filter=6";
			
			URL plUrl = new URL(plUrlStr);
			URLConnection plUrlConnection = plUrl.openConnection();
			InputStream plInputStream = plUrlConnection.getInputStream();
			String plS = ChangeCharset.inputStreamToString(plInputStream, "utf-8");
			plS = Utils.unicodeToString(plS);
			jsonObject = JSONObject.fromObject(plS);
			plS = (String) jsonObject.get("info");
			
			Parser plParser = Parser.createParser(plS, "utf-8");
			NodeFilter plFilter = new HasAttributeFilter("class","select");
			String plNumber="";
			NodeList plNodes = plParser.extractAllNodesThatMatch(plFilter);
			if(plNodes!=null) {
	          for (int i = 0; i < plNodes.size(); i++) {
	              Node textnode = (Node) plNodes.elementAt(i);
	              NodeList l = textnode.getChildren();
	              Node n = textnode.getFirstChild();
	              plNumber=n.toHtml();
	              plNumber = Utils.NoString(plNumber);
	              plNumber = plNumber.replace("评论", "");
	              plNumber = plNumber.replace("(", "");
	              plNumber = plNumber.replace(")", "");
	          }
	      }
			IndentTarget_163 indentTarget_163 = new IndentTarget_163();
	        indentTarget_163.setMsg(msg);
	        indentTarget_163.setPlNumber(plNumber);
	        indentTarget_163.setZbNumber(zbNumber);
	        return indentTarget_163;
		}

	}



